<?php
class focus_user_redis extends Common_redis_model{
	public function __construct(array $params = array()) 
	{
		parent::__construct();
		$this->params=$params;
	}
	//删除首页推荐缓存
	protected function delete_cache(){
		$keys = $this->redis->keys($this->params['channel'].":user*");
		if($keys){
			$this->redis->del($keys); 
		}
	}
	
	//关注
	public function follow($from_uid,$to_uid,$time=0) {
		if(!$this->is_following($from_uid,$to_uid)) {
			if(!$time){
				$time=time();
			}
			$this->redis->zAdd($this->params['channel'].":user:".$to_uid.":following",$time,$from_uid);
			$this->redis->zAdd($this->params['channel'].":user:".$from_uid.":followers",$time,$to_uid); 
		}
	}
	
	//取消关注
	public function unfollow($from_uid,$to_uid) {
        if ($this->is_following($from_uid,$to_uid)) {
            $this->redis->zRem($this->params['channel'].":user:".$to_uid.":following",$from_uid);//跟随者
            $this->redis->zRem($this->params['channel'].":user:".$from_uid.":followers",$to_uid); //追随者
        }
    } 
	
	//粉丝
	public function following($to_uid,$offset=0,$max="-1") {
        return $this->redis->zRevRange($this->params['channel'].":user:".$to_uid.":following", $offset, $max, TRUE); //倒序取值
    } 
	
	//他的关注
	public function followers($from_uid,$offset=0,$max="-1") {
        return $this->redis->zRevRange($this->params['channel'].":user:".$from_uid.":followers", $offset, $max, TRUE); //倒序取值
    } 
	
	
	//判断是否已关注
	public function is_following($from_uid,$to_uid)  {
        return $this->redis->zScore($this->params['channel'].":user:".$to_uid.":following", $from_uid);
    }
	
	//判断是否已关注
	public function is_followers($from_uid,$to_uid) {
        return $this->redis->zScore($this->params['channel'].":user:".$from_uid.":followers",$to_uid);
    }
	
	//粉丝数
	public function follow_count($to_uid) {
        return $this->redis->zCard($this->params['channel'].":user:".$to_uid.":following");
    } 
	
	//他的关注数
	public function follower_count($from_uid) {
        return $this->redis->zCard($this->params['channel'].":user:".$from_uid.":followers");
    }
	
	//获取粉丝
	public function get_fansi_list($to_uid,$page_num=0,$page_size=3){
		//总数
		$count = $this->follow_count($to_uid);
		//页总数
		$page_count = ceil($count/$page_size);
		$list=array();
		$temp_list=$this->redis->zRevRange($this->params['channel'].":user:".$to_uid.":following",$page_num*$page_size,($page_num*$page_size+$page_size-1));
		if($temp_list){
			foreach($temp_list as $key =>$val){
				if($val){
					$list[]=$val;
				}
			}
		}
		// return array("count" => $count, "result" => $list);
		return $list;
	}

	//获取关注
	public function get_follow_list($to_uid,$page_num=0,$page_size='-1'){
		//总数
		// $count = $this->follow_count($to_uid);
		//页总数
		// $page_count = ceil($count/$page_size);
		$list=array();

		if($page_size >= 0){
			$oldpage_size=$page_size;
			$oldpage_num=$page_num;
			$page_size = $oldpage_num*$oldpage_size+$oldpage_size-1;
			$page_num  = $oldpage_num*$oldpage_size;
		}
		
		$temp_list=$this->redis->zRevRange($this->params['channel'].":user:".$to_uid.":followers", $page_num, $page_size);
		if($temp_list){
			foreach($temp_list as $key =>$val){
				if($val){
					$list[]=$val;
				}
			}
		}
		// return array("count" => $count, "result" => $list);
		return $list;
	}
	
	public function batch_write_focus_user(){
		$this->delete_cache();
		$sql = "SELECT from_uid,to_uid,add_time FROM {$this->CI->db->dbprefix}focus_user ORDER BY id asc";
		$rslist = $this->CI->db->query($sql)->result_array();
		if($rslist){
			foreach($rslist as $key =>$val){
				$from_uid=$val["from_uid"];
				$to_uid=$val["to_uid"];
				$time=$val["add_time"];
				$this->follow($from_uid,$to_uid,$time);
			}
		}
	}

	function get_focus_key($to_uid){
		$key = $this->params['channel'].":user:".$to_uid.":following";
		$score	= $this->redis->zRevRange($key, 0, -1, FALSE);
		return $score;
	}
	
}